import createjs from 'createjs-ts';
import cjs=createjs
import Diamond from './Diamond';
export default class ExportRoot extends cjs.Container {
    private list:Array<Diamond>=[];
    private stageWidth=0;
    private stageHeight=0;
    constructor() {
        super();
        //
        this.on('added',(e:any)=>{
            this.init();
        },null,true);
        
    }
    /*private onAdded(e:any):void{
        this.init();
    }*/
    private init() {
        //console.log(this.stage.getBounds())
        const {width,height}=this.stage.getBounds();
        this.stageWidth=width;
        this.stageHeight=height;
        for(let i=0;i<30;i++){
            const diamond=new Diamond(100+Math.random()*200);
            this.reset(diamond,width,height)
            this.addChild(diamond);
            this.list.push(diamond);
        }
        this.on('tick',this.onEnterFrame.bind(this))
    }
    private onEnterFrame(e:any):void{
        this.list.forEach((diamond)=>{
            diamond.y-=diamond.speed;
            if(diamond.y<-diamond.getBounds().height){
                this.reset(diamond,this.stageWidth,this.stageHeight);
            }
        })
    }
    private reset(diamond:Diamond,width:number,height:number):void{
        diamond.speed=Math.random()*1.5+0.5;
        diamond.x=Math.random()*width;
        diamond.y=height;
    }
}